﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using BusinessLayer.Tables;
using System.Data.SqlClient;


public partial class Login : ChildPage
{
    #region Fields
    Db_Users user;
    Db_Firma firma;
    Db_Donem donem;
    #endregion

    #region Function
    private void UserName()
    {
        user = new Db_Users();
        string[] s = new string[2];
        s[0] = "usr_kod";
        s[1] = "usr_adi";
        DataTable dt = user.GetAllRecord_Datatable(s);
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            ListItem lst = new ListItem();
            lst.Value = dt.Rows[i][0].ToString();
            lst.Text = dt.Rows[i][1].ToString();
            drpKullaniciAdi.Items.Add(lst);
        }
    }
    private void FirmaKod()
    {
        firma = new Db_Firma();
        DataTable dt = firma.GetAllRecord_Datatable("frm_kod", "frm_adi");
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            ListItem lst = new ListItem();
            lst.Value = dt.Rows[i][0].ToString();
            lst.Text = dt.Rows[i][1].ToString();
            drpFirmaKodu.Items.Add(lst);
        }
    }
    private void DonemKod()
    {
        donem = new Db_Donem();
        DataTable dt = donem.GetAllRecord_Datatable("dnm_kod", "dnm_aktif_yil");
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            ListItem lst = new ListItem();
            int yil = 0;
            yil = Convert.ToInt32(dt.Rows[i][1]);
            yil += 5;
            if (yil < 10)
                lst.Text = "200" + yil.ToString();
            else
                lst.Text = "20" + yil.ToString("00");
            lst.Value = dt.Rows[i][0].ToString();
            drpDonemKod.Items.Add(lst);
        }

    }
    #endregion

    protected void Page_Load(object sender, EventArgs e)
    {
        txtPassword.Attributes.Add("onkeydown", "BtnGiris('" + btnGiris.ID + "')");
        btnIptal.Attributes.Add("onclick", "if(confirm('Programdan Çıkmak İstediğinize Eminmisiniz !')==true){ window.close() };");
    }
    protected override void OnInitComplete(EventArgs e)
    {
        UserName();
        FirmaKod();
        DonemKod();
        base.OnInitComplete(e);
    }
    protected void btnGiris_Click(object sender, EventArgs e)
    {
        user = new Db_Users();
        user.UserKod = drpKullaniciAdi.SelectedItem.Value;
        user.KayitBul();
        if (user.UserPwd == txtPassword.Text)
        {
            Random rnd = new Random();
            int num = rnd.Next(10000, 99999);
            user.UserPrgId = num.ToString();
            Session["UserRandomId"] = num.ToString();
            Session["UserKod"] = user.UserKod;
            Session["FirmaKod"] = drpFirmaKodu.SelectedValue.ToString();
            Session["DonemKod"] = drpDonemKod.SelectedValue.ToString();
            user.UserSystemIn = 1;
            user.Save();
            Response.Redirect("Default.aspx");
        }
        else
        {
            lblWarning.Visible = true;
            lblWarning.Text = "Girdiniz Kullanıcı Adı veya Şifre Yanlış";
        }
    }
}
